﻿using SuporeSliplampApp.Base;
using SuporeSliplampApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SuporeSliplampApp.Service;
using Supore.APP.UI.Controls.Notify;
using System.Text.Json.Serialization;

using System.IO;
using Newtonsoft.Json;
using System.Collections.ObjectModel;

namespace SuporeSliplampApp.ViewModels
{
    public class LoginViewModel : NotifyBase
    {
        public ObservableCollection<string> Usernames { get; set; } = new ObservableCollection<string>();

        private static LoginViewModel _loginViewModel = new LoginViewModel();
        public static LoginViewModel GetInstance()
        {
            return _loginViewModel;
        }
        LoginService loginService = new LoginService();
        public UserModel UserModel { get; set; } = new UserModel();



        private string _errorMsg;

        public string ErrorMsg
        {
            get { return _errorMsg; }
            set { _errorMsg = value; this.NotifyChanged(); }
        }

        private CommandBase _closeCommand;

        /// <summary>
        /// 关闭命令
        /// </summary>
        public CommandBase CloseCommand
        {
            get
            {
                if (_closeCommand == null)
                {
                    _closeCommand = new CommandBase();
                    _closeCommand.DoExecute = new Action<object>(obj =>
                    {
                        (obj as System.Windows.Window).DialogResult = false;
                    });
                }
                return _closeCommand;
            }

        }

        private CommandBase _minCommand;

        public CommandBase MinCommand
        {
            get
            {
                if (_minCommand == null)
                {
                    _minCommand = new CommandBase();
                    _minCommand.DoExecute += new Action<object>(obj =>
                    {
                        (obj as System.Windows.Window).WindowState = System.Windows.WindowState.Minimized;
                    });
                }
                return _minCommand;
            }
        }

        private CommandBase _loginCommand;

        public CommandBase LoginCommand
        {
            get
            {
                if (_loginCommand == null)
                {
                    _loginCommand = new CommandBase();
                    _loginCommand.DoExecute = new Action<object>(obj =>
                     {
                         try
                         {
                             if (loginService.CheckLogin(UserModel.UserName, UserModel.Password))
                             {

                                 SaveUsernameToJson(UserModel.UserName);

                                 (obj as System.Windows.Window).DialogResult = true;
                                 App.Current.Properties["UserName"] = UserModel.UserName;
                                 //SocketService.Connect();
                             }
                             else if ((UserModel.UserName == "admin") && (UserModel.Password == "lhz"))
                             {
                                 (obj as System.Windows.Window).DialogResult = true;
                                 App.Current.Properties["UserName"] = UserModel.UserName;
                             }
                             else
                             {

                                 LoginUINotify.ShowMsg(obj as System.Windows.Window);
                                 return;
                             }
                         }
                         catch (Exception)
                         {

                             throw;
                         }
                     });

                }
                return _loginCommand;

            }
        }

        private string _serverIcon = "\xe644";

        public string ServerIcon
        {
            get { return _serverIcon; }
            set
            {
                _serverIcon = value;
                this.NotifyChanged();
            }
        }

        private string _iconColor = "Red";

        public string IconColor
        {
            get { return _iconColor; }
            set
            {
                _iconColor = value;
                this.NotifyChanged();
            }
        }


        private void SaveUsernameToJson(string username)
        {
            string jsonFilePath = "usernames.json";
            List<string> usernames = new List<string>();

            // 如果文件存在，读取已有的用户名列表
            if (File.Exists(jsonFilePath))
            {
                string jsonData = File.ReadAllText(jsonFilePath);
                usernames = JsonConvert.DeserializeObject<List<string>>(jsonData) ?? new List<string>();
            }

            // 添加新用户名
            if (!usernames.Contains(username))
            {
                usernames.Add(username);
                File.WriteAllText(jsonFilePath, JsonConvert.SerializeObject(usernames));
            }
        }


        private void LoadUsernamesFromJson()
        {
            string jsonFilePath = "usernames.json";

            if (File.Exists(jsonFilePath))
            {
                string jsonData = File.ReadAllText(jsonFilePath);
                List<string> usernames = JsonConvert.DeserializeObject<List<string>>(jsonData);

                if (usernames != null)
                {
                    foreach (var username in usernames)
                    {
                        Usernames.Add(username);
                    }
                }
            }
        }

        public LoginViewModel()
        {
            LoadUsernamesFromJson(); // 加载已有用户名
        }

    }
}
